// 本模块为首页视图相关逻辑及状态,主要用来做页面传参
import vote from '@/api/vote.js'
import {  Message } from 'element-ui'
const state = {
    voteInfo: {},
    entriesCategory: [],
    entriesList: [],
    entriesPageTotal: 1,
    entriesLastPage: 1,
    entriesDetails: {},
}

const getters = {
    voteInfo(state) {
        return state.voteInfo
    },
    entriesCategory(state) {
        return state.entriesCategory
    },
    entriesList(state) {
        return state.entriesList
    },
    entriesPageTotal(state) {
        return state.entriesPageTotal
    },
    entriesLastPage(state) {
        return state.entriesLastPage
    },
    entriesDetails(state) {
        return state.entriesDetails
    },
}

const mutations = {
    setVoteState(state, { key, value }) {
        state[key] = value
    },
}
const actions = {
    getVoteInfo({ state, dispatch, commit }, data) {
        return new Promise((resolve, reject) => {

            vote.voteInfo().then((res) => {
                let code = res.data.code;
                if (code == 200) {
                    commit('setVoteState', {
                        key: 'voteInfo',
                        value: res.data.data
                    })
                    resolve(res)
                }
            }).catch((error) => {
                reject(error)
            });
        })
    },
    getEntriesCategory({ state, dispatch, commit }, data) {
        return new Promise((resolve, reject) => {
            vote.entriesCategory().then((res) => {
                let code = res.data.code;
                if (code == 200) {
                    commit('setVoteState', {
                        key: 'entriesCategory',
                        value: res.data.data
                    })
                    resolve(res)
                }
            }).catch((error) => {
                reject(error)
            });
        })
    },
    getEntriesList({ state, dispatch, commit }, data) {
        commit('setVoteState', {
            key: 'entriesList',
            value: []
        })
        commit('setVoteState', {
            key: 'entriesPageTotal',
            value: 0
        })
        commit('setVoteState', {
            key: 'entriesLastPage',
            value: 1
        })
        return new Promise((resolve, reject) => {
            vote.entriesList({
                per_page: data.per_page,
                page: data.page,
                category_id: data.category_id,
                keywords: data.keywords,
                type: data.type,//排行榜排名状态:[0:否;1:是]
            }).then((res) => {
                let code = res.data.code;
                if (code == 200) {
                    commit('setVoteState', {
                        key: 'entriesList',
                        value: res.data.data.data
                    })
                    commit('setVoteState', {
                        key: 'entriesPageTotal',
                        value: res.data.data.total
                    })
                    commit('setVoteState', {
                        key: 'entriesLastPage',
                        value: res.data.data.last_page
                    })
                    resolve(res)
                }
            }).catch((error) => {
                reject(error)
            });
        })
    },
    getEntriesDetails({ state, dispatch, commit }, data) {
        return new Promise((resolve, reject) => {
            vote.entriesDetails(data.id).then((res) => {
                let code = res.data.code;
                if (code == 200) {
                    commit('setVoteState', {
                        key: 'entriesDetails',
                        value: res.data.data
                    })
                    resolve(res)
                }
            }).catch((error) => {
                reject(error)
            });
        })
    },
    sendEntriesVote({ state, dispatch, commit }, data) {
        return new Promise((resolve, reject) => {
            vote.entriesVote({
                entries_ids: data.entries_ids
            }).then((res) => {
                let code = res.data.code;
                if (code == 200) {
                    resolve(res)
                } else {
                    Message.error(res.data.message);
                }
            }).catch((error) => {
                reject(error)

            });
        })
    },
}
export default {
    state,
    getters,
    actions,
    mutations
}